package dev.spb3.controller;

import dev.spb3.mapper.UserMapper;
import dev.spb3.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/users")
public class UserController {
    private UserMapper userMapper;

    @Autowired
    public UserController(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
    @GetMapping
    public ResponseEntity<List<User>> getAllUsers() {
        List<User> users = userMapper.getAllUsers();
        return ResponseEntity.ok(users);
    }
    @GetMapping("/{id}")
    public ResponseEntity<User> getUserById(@PathVariable int id) {
        User user = userMapper.getUserById(id);
        return ResponseEntity.ok(user);
    }
    @PostMapping
    public int insertUser(@RequestBody User user) {
        return userMapper.insertUser(user);
    }
    @PutMapping("/{id}")
    public int updateUser(@PathVariable int id,@RequestBody User user) {
        return userMapper.updateUser(user);
    }
    @DeleteMapping("/{id}")
    public int deleteUser(@PathVariable int id) {
        return userMapper.deleteUser(id);
    }
}
